import { for_obj, show_yn, obj_str, show_mt, obj_str_trim, show_st, uDb, cline, add_ck_col, loc } from 'sui';
import { load_supply } from 'stock/base/cmd';

const fix_col = () => {
    return [
        {sn: "sn", name: loc("l_488"), tp: "string"},
    ];
};

const st_list = () => {
    return [
        {sn: "open", name: show_st("open")},
        {sn: "run", name: show_st("run")},
        {sn: "close", name: show_st("close")},
    ];
};

const face_list = () => {
    return [
        {sn: "A", name: "A"},
        {sn: "B", name: "B"},
    ];
};

const yn_list = () => {
    return [
        {sn: "Y", name: show_yn("Y")},
        {sn: "N", name: show_yn("N")},
    ];
};

const get_ck = (cur, main, col) => {
    const ret = {
        sn : v => null,
    };

    add_ck_col(ret, col);

    return ret;
};

const load_stuff = (setMain, mapStuff) => load_supply("cage", setMain, mapStuff, "stuff", "gstuff");

const sort_tech = (v, all=false) => {
    let ret = v.split("-");
    let tmp = ret[0];

    if (tmp.endsWith("BTMA") || tmp.endsWith("BTMB")) {
        tmp = tmp.slice(0, -1);
    }

    if (all && ret.length === 2) {
        return `${tmp}-${ret[1]}`;
    }

    return tmp;
};

const load_tech = (setMain) => {
    uDb(null, "tech/list", { line: cline() },
        r => {
            const set = new Set();

            r.forEach(d => set.add(sort_tech(d.sn)));

            const ret = [];

            for (const d of set) {
                ret.push({ sn: d, name: d });
            }

            setMain(ret);
        },
        e => setMain(null));
};

const load_ball = (setMain) => {
    uDb(null, "tech/list", { line: cline() },
        r => {
            const set = new Set();
            const ret = [];

            r.forEach(d => {
                const ball = obj_str_trim(d, "ball");
                if (set.has(ball)) {
                    return;
                }

                ret.push({ sn: ball, name: ball });
                set.add(ball);
            });

            setMain(ret);
        },
        e => setMain(null));
};

const load_ng = (setMain, mapNg, mod="seal") => load_supply(mod, setMain, mapNg, "ng", "ngp");

const get_col = (col, tmp, map_supply, map_stuff, map_ng, is_num=false, is_ng=false) => {
    col.forEach(d => {
        if (is_ng && d.sn === "ngson") {
            return;
        }

        if (is_num && d.sn === "num") {
            return;
        }

        if (d.sn === "supply") {
            tmp.push({name: d.name, key: d.sn, width: 150, formatter: props => {
                let sn = obj_str(props.row, "supply");

                const tmp = obj_str(map_supply, sn);
                if (tmp) {
                    sn = tmp;
                }

                return sn;
            }});
        } else if (d.sn === "stuff") {
            tmp.push({name: d.name, key: d.sn, width: 150, formatter: props => {
                let sn = obj_str(props.row, "stuff");

                const tmp = obj_str(map_stuff, sn);
                if (tmp) {
                    sn = tmp;
                }

                return sn;
            }});
        } else if (d.sn === "ngson") {
            tmp.push({name: d.name, key: d.sn, width: 150, formatter: props => {
                let sn = obj_str(props.row, "ngson");

                const tmp = obj_str(map_ng, sn);
                if (tmp) {
                    sn = tmp;
                }

                return sn;
            }});
        } else {
            show_mt(tmp, d, 150);
        }
    });
};

const get_data = (umd, map_supply) => {
    const data = {};

    for_obj(umd.cur, (k, v) => {
        if (k === "supply") {
            data[k] = map_supply[v];
            return;
        }

        data[k] = v;
    });

    return data;
};

const is_box = (v) => v !== "lathe" && v !== "forge";

export {is_box, get_data, sort_tech, yn_list, get_col, load_ball, st_list, face_list, load_stuff, load_ng, load_tech, get_ck, fix_col };
